﻿#include "excelExporter.h"

ExcelExporter::ExcelExporter()
{

}


int ExcelExporter::exportExcel(QString filePath, QString fileName, QMap<QString, QString> expInfo, QVector<QStringList> expLogs, QImage* pImage){


    if(readLocalExcel()){

        QXlsx::Format format;
        format.setVerticalAlignment(QXlsx::Format::AlignVCenter);
        format.setFontName("宋体");
        format.setFontSize(10);
        format.setTextWrap(true);
        format.setBorderStyle(QXlsx::Format::BorderThin);

        // 信息页
        _xlsx->selectSheet("实验信息");
        _xlsx->write(3, 3, expInfo["ExpName"], format);     // 实验名称
        _xlsx->write(3, 5, expInfo["ExpType"], format);     // 实验类别
        _xlsx->write(3, 7, expInfo["ExpStartTime"].split(" ")[0], format);     // 实验日期
        _xlsx->write(3, 9, expInfo["EquName"], format);     // 设备名称
        _xlsx->write(4, 3, expInfo["ConsumableName"], format);     // 耗材名称
        _xlsx->write(4, 5, expInfo["ConsumableCode"], format);     // 耗材编码
        _xlsx->write(4, 7, expInfo["SampleHalfLife"], format);     // 纯化样品半衰期
        _xlsx->write(4, 9, expInfo["HalfLifeDate"], format);     // 十个半衰期后安全处理日期

        if(pImage != nullptr){
            _xlsx->insertImage(6, 1, *pImage);
        }else{
            QImage image("./tmpChromImage.png");
            if(image.load("./tmpChromImage.png")){
                image = image.scaled(720, 550);
                _xlsx->insertImage(6, 1, image);
            }
        }
        _xlsx->write(8, 2, getValveTimeStr(expLogs));


        // 流程页
        _xlsx->selectSheet("实验流程");
        bool tmpRes = _xlsx->write(2, 3, expInfo["ExpDuration"]);       // 实验总时长

        // 起始行为4
        for(int i = 0; i < expLogs.size(); i ++){
            // 校验日志数量
            for(int j = 0; j < 6; j ++){
                if(expLogs[i].size() < i){
                    expLogs[i].push_back("");
                }
            }
            _xlsx->write(4+i, 2, expLogs[i][1], format);
            _xlsx->write(4+i, 3, expLogs[i][0], format);
            _xlsx->write(4+i, 4, expLogs[i][2], format);
            _xlsx->write(4+i, 5, expLogs[i][3], format);
            _xlsx->write(4+i, 6, expLogs[i][4], format);
            _xlsx->write(4+i, 7, expLogs[i][5], format);
        }
        
        bool res = _xlsx->saveAs(filePath + "/" + fileName);
        if(!res)    return -3;

    }else{
        return -2;
    }


    return 0;
}
int ExcelExporter::exportExcel(QString filePath, QString fileName, QVariantMap expInfo, QVariantList expLogs, QImage* pImage){

    QMap<QString, QString> expInfo2;
    QVector<QStringList> expLogs2;
    foreach(QString key, expInfo.keys()){
        expInfo2[key] = expInfo[key].toString();
    }
    for(int i = 0; i < expLogs.size(); i ++){
        expLogs2.push_back(expLogs[i].toStringList());
    }
    return exportExcel(filePath, fileName, expInfo2, expLogs2, pImage);

}

QString ExcelExporter::getValveTimeStr(const QVector<QStringList> &expLogs){

    QString open13time = "";
    QString open15time = "";
    QString close13time = "";
    QString close15time = "";
    QString delay13time = "";
    QString delay15time = "";


    // 记录阀门13和15的状态，用来判断是否有打开
    bool cur13state = false;
    bool cur15state = false;


    for(int i = 0; i < expLogs.size(); i ++){

        if(expLogs.size() < 3)    continue;
        QString log = expLogs[i][2];
        QString time = expLogs[i][0];

        if(log.contains("打开阀门")){
            if(log.contains("13")){         // 13开启
                if(cur13state == false){
                    open13time = time;
                    cur13state = true;
                }
            }else{                          // 13关闭
                if(cur13state == true){
                    close13time = time;
                    cur13state = false;
                }
            }

            if(log.contains("15")){         // 15开启
                if(cur15state == false){
                    open15time = time;
                    cur15state = true;
                }
            }else{                          // 15关闭
                if(cur15state == true){
                    close15time = time;
                    cur15state = false;
                }
            }


        }

        if(log.contains("关闭全部阀门")){
            if(cur13state == true){
                close13time = time;
            }if(cur15state == true){
                close15time = time;
            }
            cur13state = false;
            cur15state = false;
        }

        if(log.contains("等待") && log.contains("秒")){
            if(i+1 < expLogs.size() && expLogs[i+1].size() >= 3){
                QString nextLog = expLogs[i+1][2];
                int time = log.replace("等待", "").replace("秒", "").toInt();
                if(nextLog.contains("打开阀门") && nextLog.contains("13")){
                    delay13time = QString::number(time);
                }if(nextLog.contains("打开阀门") && nextLog.contains("15")){
                    delay15time = QString::number(time);
                }
            }
        }
    }


    QString res = "备注: ";

    if(open13time  != "")   res.append("收集" + QString(open15time==""?"产物":"产物1") + "阀门切换时间:" + open13time + "; ");
    if(delay13time != "")   res.append("延时时长:" + delay13time + "; ");
    if(close13time != "")   res.append("阀门关闭时间:" + close13time + "; ");

    if(res != "备注: ")     res.append("  ");

    if(open15time  != "")   res.append("收集" + QString(open15time==""?"产物":"产物2") + "阀门切换时间:" + open15time + "; ");
    if(delay15time != "")   res.append("延时时长:" + delay15time + "; ");
    if(close15time != "")   res.append("阀门关闭时间:" + close15time + "; ");

    return res;



}


bool ExcelExporter::readLocalExcel(){

    _xlsx = QSharedPointer<QXlsx::Document>(new QXlsx::Document(_localExcelPath));

    if(_xlsx->load()){
        qDebug() << "打开模板文件成功";
        return true;
    }else{
        qDebug() << "打开模板文件失败";
        return false;
    }
}
